﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Modbus;
using System.Net;
using System.Net.Sockets;
using Modbus.Device;
using Modbus.Message;
using Modbus.Data;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int iport = 9600;
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] addr = ipEntry.AddressList;
            TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), iport);
            tcpListener.Start();
           var slave = ModbusTcpSlave.CreateTcp(1, tcpListener);
            slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
            for(int i=0;i<100;i++)
            {
                slave.DataStore.HoldingRegisters[i+1] = (ushort)i;
            }
            slave.Listen();
        }
    }
}
